package com.qnzw.zjmy.volley.httpUtil.rewrite;

import static android.content.Context.MODE_PRIVATE;

import android.text.TextUtils;

import com.android.volley.AuthFailureError;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.StringRequest;
import com.qnzw.zjmy.BuildConfig;
import com.qnzw.zjmy.refresh.init.GameApplication;

import java.util.HashMap;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;

public class HUCStringRequest extends StringRequest {
    private Map<String, String> requestParameter;

    public HUCStringRequest(int method, String url, Listener<String> listener,
                            ErrorListener errorListener) {
        super(method, url, listener, errorListener);
    }

    public HUCStringRequest(int method, String url, Listener<String> listener,
                            ErrorListener errorListener, Map<String, String> requestParameter) {
        super(method, url, listener, errorListener);
        this.requestParameter = requestParameter;
        if (ObjectUtil.isNotNull(this.requestParameter)) {
            this.requestParameter.put("uvChannel", BuildConfig.CHANNEL_PACKAGE);
        }
    }

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        return requestParameter;
    }

    @Override
    public byte[] getBody() throws AuthFailureError {
        if (requestParameter != null && !requestParameter.isEmpty()) {
            return new org.json.JSONObject(requestParameter).toString().getBytes();
        }
        return null;
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        long timeLong = System.currentTimeMillis();
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json; charset=utf-8");
        if (TextUtils.isEmpty(GameApplication.getMyApplication().token)) {
            String token = GameApplication.getMyApplication().getSharedPreferences("Token", MODE_PRIVATE)
                    .getString("token", "");
            headers.put("token", token);
        } else {
            headers.put("token", GameApplication.getMyApplication().token);
        }
        headers.put("uvChannel", BuildConfig.CHANNEL_PACKAGE);
//        map.put("sign", RSAEncrypt.md5Hander(timeLong));
//        map.put("timestamp", timeLong + "");
        return headers;
    }
}
